package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.dto.RoleDTO;
import com.itheima.health.dto.UserDTO;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.mapper.RoleMapper;
import com.itheima.health.mapper.UserMapper;
import com.itheima.health.pojo.Permission;
import com.itheima.health.pojo.Role;
import com.itheima.health.pojo.User;
import com.itheima.health.service.PermissionService;
import com.itheima.health.service.RoleService;
import com.itheima.health.service.UserExtService;
import com.itheima.health.service.UserService;
import com.itheima.health.vo.RoleVO;
import com.itheima.health.vo.UserVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserServiceExtImpl extends ServiceImpl<UserMapper, User> implements UserExtService {

    @Autowired
    private RoleService roleService;
    @Override
    public Result findPage(QueryPageBean queryPageBean) {

        Page<User> page=null;
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("is_delete",0);
        if (queryPageBean.getQueryString() == null) {
               page = page(new Page<User>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()),wrapper);
        }else {
            wrapper.like("username",queryPageBean.getQueryString());
            page = page(new Page<User>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()),wrapper);
        }
        PageResult pageResult = new PageResult(page.getTotal(), page.getRecords());
        Result result = new Result(pageResult);
        return result;
    }

    @Override
    public Boolean add(UserDTO userDTO) {
        Integer id = userDTO.getId();
        if (StringUtils.isNotBlank(String.valueOf(id))) {
            //根据id删除中间表数据
            baseMapper.deletePermissionInfoByRoleId(id);
        }
        Boolean flag = this.saveOrUpdate(userDTO);
        Integer gid = userDTO.getId();
        //录入中间表
        Integer[] roleIds = userDTO.getRoleIds();
        if (roleIds != null && roleIds.length > 0) {
            //中间表循环录入
            for (Integer roleId : roleIds){
                baseMapper.addRoleAndPermissionId(gid,roleId);
            }
        }
        return flag;
    }

    @Override
    public UserVO findCheckItemIdsbyGroupId(int id) {

         List<RoleVO> list = baseMapper.findAll();
        int[] roleIds= baseMapper.findPermissionIdsbyRoleId(id);
        UserVO userVO = new UserVO();
        userVO.setRoles(list);
        userVO.setRoleIds(roleIds);
        return userVO;

    }

    @Override
    public Boolean deleteById(int id) {
        User user = new User();
        user.setId(id);
        user.setIs_delete(1);
//        Role role = new Role();
//        role.setId(id);
//        role.setIs_delete(1);
        boolean flag = updateById(user);
        return flag;
    }
}
